/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bmf.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

/**
 *
 * @author Tony_
 */
@Path("requests")
@Stateless
public class rResource {

    @Context
    @EJB
    private nManagerLocal nManagerLocal;
    @Context
    UriInfo uriInfo;
    
    
    @GET
    @Produces({MediaType.TEXT_XML})
    public List<Request> getReq() {
      List<Request> r=nManagerLocal.getRequests();
      return r;
    }
    /*
	 * Questo metodo consente il salvataggio di nuovi record
	 * a fronte di una chiamata POST attraverso il form di inserimento
	 * dati
	 */
    @POST
	@Produces(MediaType.TEXT_HTML)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void newRequest(
			@FormParam("group") String group,
			@FormParam("lifetime") int lifetime,
			@FormParam("timescale") int timescale,
			@FormParam("sensor") int sensor,
			
			@Context HttpServletResponse servletResponse
	) throws IOException {
		Request r = new Request();
		r.setGroups(group);
                r.setLifetime(lifetime);
                r.setPeriod(timescale);
                r.setSensorType(sensor);
		nManagerLocal.putR(r);
		servletResponse.sendRedirect("requests.faces");
	}
    /*@PUT
    @Consumes(MediaType.APPLICATION_XML)
	public Response putRequest(JAXBElement<Request> r) {
		Request g = r.getValue();
		return putAndGetResponse(r);
	}
    
    private Response putAndGetResponse(Request r) {
	Response res;
		if(nManagerLocal.getRequests().contains(r)) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		nManagerLocal.put(g);
	return res;
	}*/
        

}